﻿
using System.Text.Json;
using System.Text.Json.Serialization;

namespace zijian666.Data;

/// <summary>
/// 定义数据库值序列化接口
/// </summary>
public interface IDbDataSerializer
{
    /// <summary>
    /// 将当前对象转换为数据库值
    /// </summary>
    /// <returns></returns>
    object Serialize();
#if NET7_0_OR_GREATER

    /// <summary>
    /// 从数据库值转换为当前对象
    /// </summary>
    /// <param name="dbValue"></param>
    /// <returns></returns>
    static abstract object Deserialize(object dbValue, Type type);
#endif
}

/// <summary>
/// 定义数据库值序列化接口
/// </summary>
public interface IDbJsonDataSerializer : IDbDataSerializer
{
    object IDbDataSerializer.Serialize()
    {
        return Serialize(this);
    }
#if NET7_0_OR_GREATER

    /// <summary>
    /// 从数据库值转换为当前对象
    /// </summary>
    /// <param name="dbValue"></param>
    /// <returns></returns>
    static object IDbDataSerializer.Deserialize(object dbValue, Type type)
    {
        return Deserialize(dbValue?.ToString() ?? string.Empty, type);
    }
#else
    public static object Deserialize(object dbValue, Type type)
    {
        return Deserialize(dbValue?.ToString() ?? string.Empty, type);
    }
#endif


    /// <summary>
    /// 默认的 JSON 序列化选项。
    /// </summary>
    private static readonly JsonSerializerOptions JSON_SERIALIZE_OPTIONS = new()
    {
        PropertyNameCaseInsensitive = true,
        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
        AllowTrailingCommas = true,
        IgnoreReadOnlyFields = true,
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
    };

    /// <summary>
    /// 将对象序列化为 JSON 字符串。
    /// </summary>
    /// <param name="obj">要序列化的对象。</param>
    /// <returns>JSON 字符串。</returns>
    private static string Serialize(object? obj)
    {
        if (obj is JsonElement { ValueKind: JsonValueKind.Undefined } element)
        {
            return string.Empty;
        }
        return JsonSerializer.Serialize(obj, JSON_SERIALIZE_OPTIONS);
    }

    /// <summary>
    /// 默认的 JSON 反序列化选项。
    /// </summary>
    private static readonly JsonSerializerOptions JSON_DESERIALIZ_OPTIONS = new()
    {
        PropertyNameCaseInsensitive = true,
        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
        AllowTrailingCommas = true,
        IgnoreReadOnlyFields = true,
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
    };

    /// <summary>
    /// 将 JSON 字符串反序列化为对象。
    /// </summary>
    /// <param name="type">目标类型。</typeparam>
    /// <param name="json">JSON 字符串。</param>
    /// <returns>反序列化后的对象。</returns>
    private static object? Deserialize(string json, Type type)
    {
        if (string.IsNullOrWhiteSpace(json))
        {
            return default;
        }
        return JsonSerializer.Deserialize(json, type, JSON_DESERIALIZ_OPTIONS);
    }

}